package com.tjl.warehouse.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.RandomUtil;
import com.google.code.kaptcha.Producer;
import com.tjl.warehouse.annotation.AuthToken;
import com.tjl.warehouse.common.R;
import com.tjl.warehouse.common.ResultUtils;
import com.tjl.warehouse.utils.RedisService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author Tang
 * @Date 2024/5/10
 */
@RestController
@RequestMapping("/captcha")
@Slf4j
@Tag(name = "验证码接口")
public class CaptchaController {

    //引入已经定义好的验证码的规则
    @Resource(name = "captchaProducer")
    private Producer captchaProducer;

    @Resource
    private RedisService redisService;


    //验证码生成
    @RequestMapping("/captchaImage")
    public void getKaptchaImage(HttpServletResponse response){
        //初始化流对象
        ServletOutputStream out = null;
        try{

            //禁止浏览器来缓存验证码图片的响应头
            response.setDateHeader("Expires",0);
            //设置响应头
            response.setHeader("Cache-Control","no-store,no-cache,must-revalidate");
            //添加请求头
            response.addHeader("Cache-Control","post-check=0,pre-check=0");
            //禁止缓存
            response.setHeader("Pragma","no-cache");

            //响应正文为jpg格式的验证码图片
            response.setContentType("image/jpeg");
            //生成验证码文本字符的内容
            String code = captchaProducer.createText();
            //将验证码文本字符保存到内存中
            BufferedImage bi = captchaProducer.createImage(code);
            //将验证码文本字符以键的形式保存到redis中
            redisService.set(code,code,180);
            log.info("验证码：{}",code);

            //将验证码返回给前端显示
            out = response.getOutputStream();
            //bi是内存中验证码图片
            ImageIO.write(bi,"jpg",out);
            //刷新
            out.flush();
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(out != null){
                    //当流使用完之后，需要关闭流
                    out.close();
                }
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    }


    //使用hutool版本
    @GetMapping("/captchaImage2")
    public R<String> captchaImage(@RequestParam("_t") String t) {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(120, 40 ,4,10);
        log.info("验证码：{}", lineCaptcha.getCode());
        //随机生成一个5位key 与图片绑定
        String key = RandomUtil.randomString(5);
        //验证码有效期3分钟
        redisService.set("captcha:" + key, lineCaptcha.getCode(), 180, TimeUnit.SECONDS);
        //把与图片存放的key放在msg当中
        return ResultUtils.success(lineCaptcha.getImageBase64Data(),key);
    }
}
